package models

import (
	"good_srv/good"
	"gorm.io/gorm"
)

type Merchant struct {
	gorm.Model
	Name     string  `gorm:"type:varchar(150);not null;commit:'商家名称'"`
	Price    float64 `gorm:"type:decimal(10,2);not null;commit:'配送费'"`
	Soles    int     `gorm:"type:int;not null;commit:'销量'"`
	Score    int     `gorm:"type:int;not null;commit:'评分'"`
	Ride     int     `gorm:"type:int;not null;commit:'距离'"`
	TypeId   int     `gorm:"type:int;not null;commit:'品类: 1-水果,2-零食'"`
	Sprice   float64 `gorm:"type:decimal(10,2);not null;commit:'起送费'"`
	Active   string  `gorm:"type:varchar(150);not null;commit:'优惠活动'"`
	Image    string  `gorm:"type:varchar(150);not null;commit:'图片'"`
	StatusId int     `gorm:"type:int;default:1;not null;commit:'商家状态: 1-在线,2-离线'"`
}
type Status struct {
	gorm.Model
	StatusName string `gorm:"type:varchar(150);not null;commit:'状态名称'"`
}
type Ms struct {
	Name       string
	Price      float64
	Soles      int
	Score      int
	Ride       int
	TypeId     int
	Active     string
	Sprice     float64
	Image      string
	StatusName string
}

func (m Merchant) MerchantList(sort string, typeId int64, sprice, price float64, active string) (merchantlists []*good.MerchantList, err error) {
	query := DB.Model(&Merchant{}).
		Select("merchants.name,merchants.price,merchants.soles,merchants.score,merchants.ride,merchants.type_id,merchants.active,merchants.sprice,merchants.image,statuses.status_name").
		Joins("left join statuses on merchants.status_id = statuses.id")

	if typeId != 0 {
		query = query.Where("type_id=?", typeId)
	} else if sprice != 0.0 {
		query = query.Where("sprice=?", sprice)
	} else if price != 0.0 {
		query = query.Where("price=?", price)
	} else if active != "" {
		query = query.Where("active=?", active)
	}

	if sort == "soles" {
		query = query.Order("soles desc")
	} else if sort == "score" {
		query = query.Order("score desc")
	} else if sort == "ride" {
		query = query.Order("ride desc")
	}
	query.Find(&merchantlists)
	return
}
